export { setAppConfig, getAppConfig, removeAppConfig } from './cache/app-config.cache'
export { setAccessToken, getAccessToken, removeAccessToken } from './cache/token.cache'
export { setRefreshToken, getRefreshToken, removeRefreshToken } from './cache/token.cache'
export { setLoginParams, getLoginParams, removeLoginParams } from './cache/remember-me.cache'
export { setSidebarStatus, getSidebarStatus, removeSidebarStatus } from './cache/sidebar-status.cache'
export { setCachedViews, getCachedViews, setVisitedViews, getVisitedViews } from './cache/tags-view.cache'

export { CacheKey } from './enum/cache-key.enum'
export { ContentType } from './enum/content-type.enum'
export { GenderEnum } from './enum/gender.enum'
export { TimeFormat } from './enum/time-format.enum'
export { TimeUnit } from './enum/time-unit.enum'

export { extractFilename, linkDownload } from './helper/request.helper'

export { LocalCache } from './class/local-cache.class'
export { TipModal } from './class/tip-modal'
export { transformListToTree, transformTreeToList } from './tree-helper'

/** 取得 [n, m] 范围内随机整数 */
export function randomFullClose(min: number, max: number): number {
  if (min > max) throw new Error(`min must be less than max!`)
  return Math.floor(Math.random() * (max - min + 1) + min)
}

/** 延时函数 指定 ms 后再执行后续函数 */
export function sleep(delay = 1000): Promise<void> {
  return new Promise((resolve, _) => setTimeout(() => resolve(), delay))
}

/** 用 JS 获取全局 CSS 变量 */
export function getCssVariableValue(cssVariableName: string): string {
  return getComputedStyle(document.documentElement).getPropertyValue(cssVariableName)
}

/** 用 JS 设置全局 CSS 变量 */
export function setCssVariableValue(cssVariableName: string, cssVariableValue: string): void {
  document.documentElement.style.setProperty(cssVariableName, cssVariableValue)
}

/** 获取变量的真实类型 */
export function getRawType(variable: any): VariableType {
  return Object.prototype.toString.call(variable).split(' ')[1].replace(']', '').toLowerCase() as VariableType
}

/** 反转字符串 */
export function reverseString(str: string): string {
  return str.split('').reverse().join('')
}

/** 驼峰字符串转下划线字符串 */
export function humpToUnderline(str: string): string {
  return str.replace(/([A-Z])/g, '_$1').toLowerCase()
}

/** 下划线字符串转驼峰字符串  */
export function underlineToHump(str: string): string {
  return str.replace(/\_(\w)/g, (_, letter: string) => letter.toUpperCase())
}

/** 驼峰字符串转中划线字符串 */
export function humpToDash(str: string): string {
  return str.replace(/([A-Z])/g, '-$1').toLowerCase()
}

/** 中划线字符串转驼峰字符串 */
export function dashToHump(str: string): string {
  return str.replace(/\-(\w)/g, (_, letter: string) => letter.toUpperCase())
}

/** 通过名称获取 URL 查询参数 */
export function getQueryByName(name: string): string {
  const queryNameRegExp = new RegExp(`[?&]${name}=([^&]*)(?:&|$)`)
  const queryNameMatch = window.location.href.match(queryNameRegExp)
  return queryNameMatch ? decodeURIComponent(queryNameMatch[1]) : ''
}

/** 查询字符串转对象 */
export function getQueryObject(url?: string): Record<string, string> {
  url = url ? url : window.location.href
  const search = url.substring(url.lastIndexOf('?') + 1)
  const query: Record<string, string> = {}
  const list = search.split('&')
  for (const item of list) {
    if (!item.includes('=')) continue
    const [key, value] = item.split('=')
    query[key] = value
  }
  return query
}

/**
 * 获取当前设备的屏幕 DPI（每英寸像素数）
 *  此函数通过创建一个宽度为 1 英寸的临时不可见 div 元素，
 *  将其添加到文档中后读取其 offsetWidth 属性来计算当前设备的 DPI，然后移除该元素。
 * @returns {number} 当前设备的 DPI（即 1 英寸所对应的像素数）
 */
export function getDeviceDpi(): number {
  const tempDiv = document.createElement('div')
  tempDiv.style.width = '1in'
  tempDiv.style.visibility = 'hidden'
  document.body.appendChild(tempDiv)
  const dpi = tempDiv.offsetWidth
  document.body.removeChild(tempDiv)
  return dpi
}
